<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
//
// ===============================================================================
// Add-On: All Invoices v1.1
// Designed for: Zen Cart 1.3.x series
// Created by: Mathew O'Marah (www.mdodesign.co.uk)
//
// Donations:  Please support Zen Cart!  paypal@zen-cart.com  - Thank you!
// ===============================================================================


require(DIR_WS_CLASSES . 'currencies.php');
$currencies = new currencies();

include(DIR_WS_CLASSES . 'order.php');
?>

<?php
global $db;
$start = $_GET["start"];
$end = $_GET["end"];
$days = getDatesBetween2Dates($start, $end);
//var_dump($days);
$total_all_din = 0.0;
$total_all_mul = 0.0;
$total_all_out = 0.0;

$total_orders_din = 0;
$total_orders_mul = 0;
$total_orders_out = 0;

foreach($days as $day)
{
	
	$orders_din = $db->Execute("select h.orders_id, t.text, t.value
                               from " . TABLE_ORDERS_STATUS_HISTORY . " h 
								join " . TABLE_ORDERS_TOTAL . " t on h.orders_id = t.orders_id
                               where t.class = 'ot_total' and orders_status_id = '4' and cast(h.date_added as date) = '" . $day['sql'] . "' and h.comments = 'Dinheiro'");
	$orders_mul = $db->Execute("select h.orders_id, t.text, t.value
                               from " . TABLE_ORDERS_STATUS_HISTORY . " h 
								join " . TABLE_ORDERS_TOTAL . " t on h.orders_id = t.orders_id
                               where t.class = 'ot_total' and orders_status_id = '4' and cast(h.date_added as date) = '" . $day['sql'] . "' and h.comments = 'Multibanco'");
	$orders_out = $db->Execute("select h.orders_id, t.text, t.value, h.comments
                               from " . TABLE_ORDERS_STATUS_HISTORY . " h 
								join " . TABLE_ORDERS_TOTAL . " t on h.orders_id = t.orders_id
                               where t.class = 'ot_total' and orders_status_id = '4' and cast(h.date_added as date) = '" . $day['sql'] . "' and (h.comments <> 'Dinheiro' and h.comments <> 'Multibanco')"
							);	
	$row_count = max($orders_din->RecordCount(), $orders_mul->RecordCount(), $orders_out->RecordCount());	
	
	if($row_count > 0)
	{
		$total_din = 0.0;
		$total_mul = 0.0;
		$total_out = 0.0;
		?>
<p class="pageHeading"><?php echo $day['show']; ?></p>
<table width="800px" cellspacing="0" cellpadding="2" border="1">	
	<tr>
		<td colspan="2" width="25%">Dinheiro</td>
		<td colspan="2" width="25%">Multibanco</td>
		<td colspan="3" width="50%">Outros</td>
	</tr>
		<?php 
		for ($i = 0; $i < $row_count; $i++)
		{
			echo("<tr>");
			//show cell of Dinheiro
			if(!$orders_din->EOF) 
			{
				$total_din += (str_replace('&euro;', '', $orders_din->fields['text']));
				echo("<td width='100px'>" . $orders_din->fields['orders_id'] . "</td><td width='100px'>" . str_replace('&euro;', '', $orders_din->fields['text']) .  "</td>");
				$orders_din->MoveNext();
			}
			else echo("<td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td>");
			
			//show cell of Multibanco
			if(!$orders_mul->EOF) 
			{
				$total_mul += (str_replace('&euro;', '', $orders_mul->fields['text']));
				echo("<td width='100px'>" . $orders_mul->fields['orders_id'] . "</td><td width='100px'>" . str_replace('&euro;', '', $orders_mul->fields['text']) .  "</td>");
				$orders_mul->MoveNext();
			}
			else echo("<td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td>");
			//show cell of Outros
			if(!$orders_out->EOF)
			{
				$total_out += (str_replace('&euro;', '', $orders_out->fields['text']));
				echo("<td width='100px'>" . $orders_out->fields['orders_id'] . "</td><td width='100px'>" . str_replace('&euro;', '', $orders_out->fields['text']) .  "</td><td width='200px'>" . $orders_out->fields['comments'] .  "</td>");
				$orders_out->MoveNext();
			}
			else echo("<td width='100px'>&nbsp;</td><td width='100px'>&nbsp;</td><td width='200px'>&nbsp;</td>");
			echo("</tr>");
		}
		$total_all_din += $total_din;
		$total_all_mul += $total_mul;
		$total_all_out += $total_out;
		
		$total_orders_din += $orders_din->RecordCount();
		$total_orders_mul += $orders_mul->RecordCount();
		$total_orders_out += $orders_out->RecordCount();
		
		echo ("<tr><td>" . $orders_din->RecordCount() . "</td><td>" . $total_din . "</td>
				<td>" . $orders_mul->RecordCount() . "</td><td>" . $total_mul . "</td>
				<td>" . $orders_out->RecordCount() . "</td><td colspan='2'>" . $total_out . "</td></tr>");
		echo ("<tr><td colspan='3'>" . ($orders_din->RecordCount() + $orders_mul->RecordCount() + $orders_out->RecordCount()) . "</td>
				<td colspan='4'>" . ($total_din + $total_mul + $total_out) . "</td>"); 
	?>
</table><div>&nbsp;</div>
	<?php 
	}
	
}


?>
<?php if ($total_orders_din > 0 || $total_orders_mul > 0 || $total_orders_out > 0) {?>
<h1>Resumo</h1>
<table width="600px" cellspacing="0" cellpadding="2" border="1">
	<tr><td width="150px"><?php echo $total_orders_din;?></td><td width="150px"><?php echo $total_orders_mul;?></td><td width="150px"><?php echo $total_orders_out;?></td>
		<td width="150px"><?php echo ($total_orders_din + $total_orders_mul + $total_orders_out);?></td>    	
	</tr>
	<tr><td><?php echo $total_all_din;?></td><td><?php echo $total_all_mul;?></td><td><?php echo $total_all_out;?></td><td><?php echo ($total_all_din + $total_all_mul + $total_all_out);?></td></tr>
</table>
<?php }?>
<?php
function getDatesBetween2Dates($startTime, $endTime) {
	$day = 86400;
	$formatSQL = 'Y-m-d';
	$formatShow = 'd/m/Y';
	$startTime = strtotime($startTime);
	$endTime = strtotime($endTime);
	$numDays = round(($endTime - $startTime) / $day) + 1;
	$days = array();

	for ($i = 0; $i < $numDays; $i++) {
		$days[] = array('sql' => date($formatSQL, ($startTime + ($i * $day))), 'show' => date($formatShow, ($startTime + ($i * $day))));
	}

	return $days;
} 
?>

